#include <OsgClassDataSource/GeodeOsg.h>
#include <iostream>
#include <osg/ValueObject>
GeodeOsg::GeodeOsg( QObject *parent /*= 0*/ ) : NodeOsg(parent)
{
    m_object = new osg::Geode;

    m_object->setUserValue("geodeIndex",-1);
    this->refreshInfoContainer();
}

GeodeOsg::~GeodeOsg()
{

}

int GeodeOsg::getGeodeIndex()
{
    int index = -1;
    osg::Geode* geodePtr = dynamic_cast<osg::Geode*>(m_object.get());
    if(geodePtr != NULL)
    {
        if(!geodePtr->getUserValue("geodeIndex",index))
            index = -1;
    }
    return index;
}

void GeodeOsg::setGeodeIndex(int index)
{
    osg::Geode* geodePtr = dynamic_cast<osg::Geode*>(m_object.get());
    if(geodePtr != NULL)
    {
        geodePtr->setUserValue("geodeIndex",index);
    }

}

void GeodeOsg::replaceOsgObject(osg::ref_ptr<osg::Object> object)
{
    NodeOsg * parentNodeOsg = getParentNodeOsg();
    osg::Group* parentGroup = NULL;
    if(parentNodeOsg != NULL)
    {
        parentGroup = dynamic_cast<osg::Group*>(parentNodeOsg->getObject().get());
    }
    osg::Geode* oldGeodePtr = dynamic_cast<osg::Geode*>(m_object.get());
    ObjectOsg::replaceOsgObject(object);
    osg::Geode* geodePtr = dynamic_cast<osg::Geode*>(m_object.get());
    if(geodePtr != NULL && oldGeodePtr != NULL && parentGroup != NULL)
    {
        //parentGroup->removeChild(oldGeodePtr);
        parentGroup->replaceChild(oldGeodePtr, geodePtr);
    }

}
